package cn.anosi.controller.bpm;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.anosi.controller.BaseController;
import cn.anosi.entity.bpm.ProcessDefinitionVo;
import cn.anosi.page.PageData;

@Controller
@RequestMapping("/processDefinition")
public class ProcessDefinitionController extends BaseController {
	/**
	 * 审批流程管理页面
	 */
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	public String page() {
		return "bpm/processDefinition_list";
	}

	/**
	 * 查询最新的流程定义列表
	 * 
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public PageData<ProcessDefinitionVo> list() throws IllegalAccessException, InvocationTargetException {
		ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
		query.orderByProcessDefinitionVersion().asc();
		List<ProcessDefinition> list = query.list();
		Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();
		for (ProcessDefinition pd : list) {
			map.put(pd.getKey(), pd);
		}
		ArrayList<ProcessDefinitionVo> arrayList = new ArrayList<>();
		for (ProcessDefinition p : map.values()) {
			ProcessDefinitionVo vo = new ProcessDefinitionVo();
			BeanUtils.copyProperties(vo, p);
			arrayList.add(vo);
		}
		PageData<ProcessDefinitionVo> pageData = new PageData<>();
		pageData.setData(arrayList);
		return pageData;
	}

	/**
	 * 删除流程定义
	 * @param deploymentId 部署id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public int delete(String deploymentId) {
		try {
			repositoryService.deleteDeployment(deploymentId, true);
			return 1;
		} catch (Exception e) {
			return -1;
		}
	}

	/**
	 * 查看流程定义图
	 * @param processDefinitionId
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public void view(String processDefinitionId, HttpServletResponse response) throws Exception {
		InputStream is = repositoryService.getProcessDiagram(processDefinitionId);
		byte[] buffer = new byte[is.available()];
		is.read(buffer);
		response.setContentType("image/png");
		OutputStream os = response.getOutputStream();
		os.write(buffer);
		is.close();
		os.close();
	}
	
	/**
	 * 部署流程定义
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/deploy", method = RequestMethod.POST)
	@ResponseBody
	public String deploy(MultipartFile file) throws IOException{
		if(file != null){
			DeploymentBuilder builder = repositoryService.createDeployment();
			ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream());
			builder.addZipInputStream(zipInputStream);
			
			Deployment deploy = builder.deploy();
			String id = deploy.getId();
			file.getInputStream().close();
			zipInputStream.close();
			if(StringUtils.isNotBlank(id)){
				logger.debug("部署id:" + id);
				return "success";
			}
			
			return "fail";
		}
		return "fail";
	}
}
